package com.qk.management.service.impl;

import com.qk.common.PageResult;
import com.qk.entity.Role;
import com.qk.management.mapper.RoleMapper;
import com.qk.management.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 角色业务层实现类
 *
 * @Author:Mr.m
 * @Date: 2025-10-07 1:28
 * @Description: TODO
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    //查询所有角色
    @Override
    public List<Role> list() {
        return roleMapper.selectAll();

    }

    //删除角色
    @Override
    public void deleteById(Integer id){
        roleMapper.deleteById(id);
    }

    //修改角色
    @Override
    public void update(Role role) {
        // 设置更新时间
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.update(role);
    }

    //根据id查询角色
    @Override
    public Role queryRoleById(Integer id) {
        Role role = roleMapper.queryRoleById(id);
        return role;
    }

    //根据页面条件查询
    @Override
    public PageResult<Role> page(String name, Integer page, Integer pageSize) {
        //1.查询满足条件的总记录数
        Integer total = roleMapper.count(name);
        //2.查询满足条件的当前页数据
        //计算偏移量
        Integer offset = (page - 1) * pageSize;
        List<Role> rows = roleMapper.selectByPage(name, offset, pageSize);
        //3.数据组装
        return PageResult.<Role>builder()
                .total(total)
                .rows(rows)
                .build();
    }

    //新增角色
    @Override
    public void addRole(Role role) {
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.insert(role);
    }
}
